package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.InterfaceDTO;
import com.itlong.cloud.POJO.PO.InterfacePO;
import com.itlong.cloud.POJO.VO.operate.InterfaceVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.operate.dao.IOperateInterfaceDao;
import com.itlong.cloud.operate.service.IOperateInterfaceService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * <desc>
 *     运营-接口类型-实现类
 * <desc/>
 * Author LiCHangXu
 * Date 2021-8-16
 */
@Service
public class OperateInterfaceServiceImpl implements IOperateInterfaceService {

    @Autowired
    IOperateInterfaceDao iOperateInterfaceDao;


    /**
     * 添加接口实现
     * @param interfacePO
     * @return
     * @throws Exception
     */
    @Override
    public Integer save(InterfacePO interfacePO) throws Exception{
        String id = ("00000"+String.valueOf(new Random().nextInt(100000)));
        String num = id.substring(id.length()-5);
        interfacePO.setInterfaceNum(num);
        interfacePO.setInterfaceId(LogicIdUtil.bussinessId());
        interfacePO.setCreateTime(new Date());
        return iOperateInterfaceDao.save(SqlUtil.durableData(interfacePO, PlatformConstants.TABLE_SAVE));
    }

    /**
     *  修改接口实现
     * @param interfacePO
     * @return
     * @throws Exception
     */
    @Override
    public Integer update(InterfacePO interfacePO) throws Exception{
        interfacePO.setUpdateTime(new Date());
        return iOperateInterfaceDao.update(SqlUtil.durableData(interfacePO,PlatformConstants.TABLE_UPDATE));
    }

    /**
     * 删除接口实现
     * @param interfaceId
     * @param updateUserId
     * @return
     */
    @Override
    public Integer delete(String interfaceId,String updateUserId){
        String[] ids = interfaceId.split(",");
        return iOperateInterfaceDao.deletes(ids,updateUserId,new Date());
    }

    /**
     * 查询接口实现
     * @param interfaceName
     * @param status
     * @param sTime
     * @param eTime
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<InterfacePO> getList(String interfaceName, int status, String sTime, String eTime)throws Exception{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        try{
            startTime = simpleDateFormat.parse(sTime);
        }catch (Exception e){
        }
        try{
            endTime = simpleDateFormat.parse(eTime);
        }catch (Exception e){
        }
        return iOperateInterfaceDao.getList(interfaceName,status,startTime,endTime);
    }

    /**
     * 条件查询接口实现、支持分页
     * @param interfaceDTO
     * @return
     * @throws Exception
     */
    @Override
    public Page<InterfaceVO> getInterfacePageList(InterfaceDTO interfaceDTO) throws Exception{
        Page<InterfaceVO> voPage = new Page<>();
        voPage.getPage().setPageSize(interfaceDTO.getPageSize());
        voPage.getPage().setCurrentPage(interfaceDTO.getCurrentPage());
        Map<String,Object> mapDto = SqlUtil.convertBeanToMapParams(interfaceDTO);
        mapDto.put("startSlip",voPage.getPage().getStartSlip());
        mapDto.put("pageSize",voPage.getPage().getPageSize());
        List<InterfacePO> interfacePOList = iOperateInterfaceDao.getInterfacePageList(mapDto);
        List<InterfaceVO> interfaceVOList = new ArrayList<>();
        for(InterfacePO ifp:interfacePOList){
            InterfaceVO interfaceVO = new InterfaceVO();
            BeanUtils.copyProperties(ifp,interfaceVO);
            interfaceVOList.add(interfaceVO);
        }
        voPage.setRows(interfaceVOList,iOperateInterfaceDao.getPageCount(mapDto));
        return voPage;
    }


    /**
     * 更新接口启用-禁用
     * @param interfaceId 接口id
     * @param status 接口状态
     * @param updateUserId 更新者id
     * @return
     * @throws Exception
     */
    @Override
    public Integer updateStatus(String interfaceId, int status,String updateUserId) throws Exception{
        InterfacePO interfacePO = new InterfacePO();
        interfacePO.setInterfaceId(interfaceId);
        interfacePO.setStatus(status);
        interfacePO.setUpdateTime(new Date());
        interfacePO.setUpdateUserId(updateUserId);
        return iOperateInterfaceDao.update(SqlUtil.durableData(interfacePO,PlatformConstants.TABLE_UPDATE));
    }

    /**
     * 修改接口文档实现
     * @param interfaceId 接口id
     * @param interfaceDocx 接口文档
     * @param updateUserId 修改用户id
     * @return
     * @throws Exception
     */
    @Override
    public Integer updateDocx(String interfaceId, String interfaceDocx,String updateUserId) throws Exception {
        InterfacePO interfacePO = new InterfacePO();
        interfacePO.setInterfaceId(interfaceId);
        interfacePO.setInterfaceDocx(interfaceDocx);
        interfacePO.setUpdateTime(new Date());
        interfacePO.setUpdateUserId(updateUserId);
        return iOperateInterfaceDao.update(SqlUtil.durableData(interfacePO,PlatformConstants.TABLE_UPDATE));
    }


}
